package com.tboss.template.system.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.tboss.template.system.dto.FunctionDto;
import com.tboss.template.system.dto.PrivateGrpFnDto;
import com.tboss.template.system.service.FunctionService;

@Controller
@RequestMapping(value = "/template/system/function")
public class FunctionController {
	
	@Autowired private FunctionService fnService;

	@RequestMapping(value = {"/_list{reqPage}", "/_list/{reqPage}", "/_listConts/{reqPage}"}, 
			method = {RequestMethod.POST, RequestMethod.GET})//
	public String fnList(@PathVariable("reqPage") Integer reqPage, @ModelAttribute FunctionDto dto,
			Model model, HttpServletRequest req){

		String rtnUrl = req.getServletPath().substring(1);		
		if(reqPage != null) rtnUrl = rtnUrl.substring(0, rtnUrl.lastIndexOf("/"));
		
		this.fnService.setModelFnList(reqPage, model, dto);
		
		return rtnUrl+"Ajax";
	}
	
	@RequestMapping(value = {"/_regForm"}, method = {RequestMethod.POST, RequestMethod.GET})//
	public String fnRegForm(Model model, @ModelAttribute FunctionDto dto, HttpServletRequest req){
		String rtnUrl = req.getServletPath().substring(1);
		
		this.fnService.fnRegForm(model, dto);

		return rtnUrl+"Ajax";
	}

	@RequestMapping(value = {"/_regFormSave"}, method = {RequestMethod.POST})//
	public Model fnRegFormSave(@ModelAttribute FunctionDto dto, Model model
			, @ModelAttribute("items") PrivateGrpFnDto items){

		this.fnService.addFnInfo(dto, items);

		return model;
	}
	
	@RequestMapping(value = {"/_regFormRevise/{fnId}"}, method = {RequestMethod.POST, RequestMethod.GET})//
	public String fnRegFormRevise(Model model, @PathVariable("fnId") String fnId, @ModelAttribute FunctionDto dto, HttpServletRequest req){
		String rtnUrl = req.getServletPath().substring(1);
		if(fnId != null) rtnUrl = rtnUrl.substring(0, rtnUrl.lastIndexOf("Revise/"+fnId));
		
		this.fnService.fnRegForm(model, fnId, dto);
		
		return rtnUrl+"Ajax";
	}
	
	@RequestMapping(value = {"/_regFormReviseSave"}, method = {RequestMethod.POST})//
	public Model fnRegFormReviseSave(@ModelAttribute FunctionDto dto, Model model
			, @ModelAttribute("items") PrivateGrpFnDto items){
		
		this.fnService.updateFnInfo(dto, items);
		
		return model;
	}
	
	@RequestMapping(value = {"/_detail/{fnId}"}, method = {RequestMethod.POST, RequestMethod.GET})//
	public String fnInfoDetail(Model model, @PathVariable("fnId") String fnId, @ModelAttribute FunctionDto dto, HttpServletRequest req){
		String rtnUrl = req.getServletPath().substring(1);
		if(fnId != null) rtnUrl = rtnUrl.substring(0, rtnUrl.lastIndexOf("/"+fnId));
		
		this.fnService.fnRegForm(model, fnId, dto);
		
		return rtnUrl+"Ajax";
	}
	
	@RequestMapping(value = {"/_groups/{fnId}"}, method = {RequestMethod.POST, RequestMethod.GET})//
	public String fnInfoGroups(Model model, @PathVariable("fnId") String fnId, HttpServletRequest req){
		String rtnUrl = req.getServletPath().substring(1);
		if(fnId != null) rtnUrl = rtnUrl.substring(0, rtnUrl.lastIndexOf("/"+fnId));
		
		this.fnService.fnGroups(model, fnId);
		
		return rtnUrl+"Ajax";
	}
	
}
